<?php
// +----------------------------------------------------------------------
// | 微信回复配置
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2014 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 赵杰
// +----------------------------------------------------------------------

namespace Weixin\Controller;

use Common\Controller\ShuipFCMS;

class responseController extends ShuipFCMS 
{
	//文字回复Xml
	public $textXml="<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[%s]]></MsgType>
					<Content><![CDATA[%s]]></Content>
					</xml>";
	
	//图文回复
	public $imgXml="<xml>
					 <ToUserName><![CDATA[%s]]></ToUserName>
					 <FromUserName><![CDATA[%s]]></FromUserName>
					 <CreateTime>%s</CreateTime>
					 <MsgType><![CDATA[%s]]></MsgType>
					 <ArticleCount>1</ArticleCount>
					 <Articles>
					 <item>
					 <Title><![CDATA[%s]]></Title> 
					 <Description><![CDATA[%s]]></Description>
					 <PicUrl><![CDATA[%s]]></PicUrl>
					 <Url><![CDATA[%s]]></Url>
					 </item>
					 </Articles>
					 </xml>";
		//图文回复
	public $imgXml_all="<xml>
					 <ToUserName><![CDATA[%s]]></ToUserName>
					 <FromUserName><![CDATA[%s]]></FromUserName>
					 <CreateTime>%s</CreateTime>
					 <MsgType><![CDATA[%s]]></MsgType>
					 <ArticleCount>1</ArticleCount>
					 <Articles>
					 <item>
					 <Title><![CDATA[%s]]></Title> 
					 <Description><![CDATA[%s]]></Description>
					 <PicUrl><![CDATA[%s]]></PicUrl>
					 <Url><![CDATA[%s]]></Url>
					 </item>
					 <item>
					 <Title><![CDATA[%s]]></Title> 
					 <Description><![CDATA[%s]]></Description>
					 <PicUrl><![CDATA[%s]]></PicUrl>
					 <Url><![CDATA[%s]]></Url>
					 </item>
					 <item>
					 <Title><![CDATA[%s]]></Title> 
					 <Description><![CDATA[%s]]></Description>
					 <PicUrl><![CDATA[%s]]></PicUrl>
					 <Url><![CDATA[%s]]></Url>
					 </item>
					 </Articles>
					 <FuncFlag>1</FuncFlag>
					 </xml>";	
	
	public function responseMsg($postStr,$signature)
	{
		if (!empty ($postStr)) {
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$fromUsername = $postObj->FromUserName;
			$toUsername = $postObj->ToUserName;
            $msgType=$postObj->MsgType;
			$EventKey = $postObj->EventKey;
			$Event = $postObj->Event;
			$content = $postObj->Content;
			$time = time();
			//关注自动回复开始
			if($Event=="subscribe"){
				return $this->getConcern($toUsername,$fromUsername,$msgType,$Event);
			}
			if($Event == "unsubscribe"){
				
			}
			if($Event == "CLICK"){
				$tmp = M('wp_keyword');
				$where="and keyword = '{$EventKey}'";
				$info = $tmp->where("token='".THIS_TOKEN."' {$where}")->find();
				file_put_contents('./shuipf/Application/Weixin/Controller/ccc.txt', $tmp->getlastsql());
				if($info){	
					return $this->getMenuInfo($info, $toUsername, $fromUsername, $msgType, $Event);
				}
			}
			//关注自动回复结束
			
			//文本回复
			if($content){
				$tmp = M('wp_keyword');
				$where="and keyword like '%{$content}%'";
				$info = $tmp->where("token='".THIS_TOKEN."' {$where}")->order(array('cTime' => 'DESC'))->select();
				if($info){
					return $this->getMessage($info,$content,$toUsername,$fromUsername,$msgType,$Event);
				}
			}
			
		}
		else {
			return "";
		}
	}
	
	//文本回复
	public function getMenuInfo($info,$toUsername,$fromUsername,$msgType,$Event){
		$where['id'] = array('eq',$info['aim_id']);
		$table = substr($info['addon'],4);
		$tmp = M($table);
		$content = $tmp->where($where)->find();
		file_put_contents('./shuipf/Application/Weixin/Controller/bbb.txt', $tmp->getlastsql());
		if($content){
			if($info['addon'] == "jbr_wp_custom_reply_news"){
				$msgType = "news";
				$time = time();
				$title=$content['title'];
				$Description=$content['intro'];
				$PicUrl="http://".$_SERVER['HTTP_HOST'].$content['cover'];
				$Url=$content['jump_url'];
				$resultStr = sprintf($this->imgXml, $fromUsername, $toUsername, $time,$msgType, $title,$Description,$PicUrl,$Url);
				return $resultStr;
				exit;
			}
		}
	}
	
	//获取关注时回复的数据
	public function getConcern($toUsername,$fromUsername,$msgType,$Event)
	{
		//推送到微信的XML数据格式
		$tmp = M('wp_member_public');
		$content = $tmp->where("token='".THIS_TOKEN."'")->find();
		$tmps = json_decode($content['addon_config'],true);
		$msgType = "text";
		if($tmps['Wecome']['url']){
			$url = $tmps['Wecome']['url'];
		}
		else{
			$url = "http://".$_SERVER['HTTP_HOST'];
		}
		$contentStr = "<a href='{$url}' >".$tmps['Wecome']['description']."</a>";
		$resultStr = sprintf($this->textXml, $fromUsername, $toUsername, $time, $msgType, $contentStr);
		return $resultStr;
	}
	//文本回复
	public function getMessage($info,$content,$toUsername,$fromUsername,$msgType,$Event){
		foreach($info as $key=>$value){
			if($value['keyword_type'] == 0){
				$where['keyword'] = array('like',"{$content}");
			}
			elseif($value['keyword_type'] == 1){
				$where['keyword'] = array('like',"%{$content}");
			}
			elseif($value['keyword_type'] == 2){
				$where['keyword'] = array('like',"{$content}%");
			}
			elseif($value['keyword_type'] == 3){
				$where['keyword'] = array('like',"%{$content}%");
			}
			$where['id'] = array('eq',$value['aim_id']);
			$table = substr($value['addon'],4);
			$tmp = M($table);
			$content = $tmp->where($where)->find();
			if($content){
				if($value['addon'] == "jbr_wp_custom_reply_text"){
					$msgType = "text";
					$time = time();
					$contentStr = $content['content'];
					$resultStr = sprintf($this->textXml, $fromUsername, $toUsername, $time, $msgType, $contentStr);
					return $resultStr;
					exit;
				}
				if($value['addon'] == "jbr_wp_custom_reply_news"){
					$msgType = "news";
					$time = time();
					$title=$content['title'];
					$Description=$content['intro'];
					$PicUrl="http://".$_SERVER['HTTP_HOST'].$content['cover'];
					$Url=$content['jump_url'];
					$resultStr = sprintf($this->imgXml, $fromUsername, $toUsername, $time,$msgType, $title,$Description,$PicUrl,$Url);
					return $resultStr;
					exit;
				}
				if($value['addon'] == "jbr_wp_custom_reply_mult"){
					return $this->getMoreMsg($content['mult_ids'],$toUsername,$fromUsername,$msgType,$Event);
					exit;
				}
				
			}
		}
	}
	
	protected function getMoreMsg($ids,$toUsername,$fromUsername,$msgType,$Event){
		$where['id'] = array('in',$ids);
		$content = D('wp_custom_reply_news')->where($where)->select();
		$count = count($content);
		$msgType = "news";
		$resultStr ="<xml>
					<ToUserName><![CDATA[{$fromUsername}]]></ToUserName>
					<FromUserName><![CDATA[{$toUsername}]]></FromUserName>
					<CreateTime>{$time}</CreateTime>
					<MsgType><![CDATA[news]]></MsgType>
					<ArticleCount>{$count}</ArticleCount>
					<Articles>";
		foreach($content as $key=>$value){
			$PicUrl="http://".$_SERVER['HTTP_HOST'].$value['cover'];
			$resultStr .= "<item>
			<Title><![CDATA[{$value['title']}]]></Title>
			<Description><![CDATA[{$value['intro']}]]></Description>
			<PicUrl><![CDATA[{$PicUrl}]]></PicUrl>
			<Url><![CDATA[{$value['jump_url']}]]></Url>
			</item>";
		}	
		$resultStr .= "</Articles></xml>";	
		return $resultStr;
	}
	
}
